package group6renderer.renderernode;

import group6renderer.Display;
import group6renderer.HTMLConstants;
import group6renderer.Visitor;

/** ClASS NAME: HeadingNode
 * DESCRIPTION: An class to deal with headings
 * COLABORATORING CLASSES: BranchNode, Display, Renderer
 * @author jonnie
 **/
public class HeadingNode extends BranchNode
{

    private String heading;

    /**
	 * A Constructor to initialise the fields of a new HeadingNode.
	 **/
    public HeadingNode(String heading)
    {
	this.heading = heading;
	name = heading;
    }

    /** 
     * A method overriding the abstract method in TreeNode. It adjusts the properties of the State class according to the type of node
     * concerned.  
     * @param disp the Display object of the project (there is only one)
     * @Override
     */
    public void drawNode(Display disp)
    {
	

	if (goingDown && !isCopy)
	{
	    if (lineChange1())
	    {
		disp.crlf();
	    }
	    else
	    {
		disp.crlf();
		disp.crlf();
	    }

	    disp.getState().toggleState(HTMLConstants.HEADING,true);
	    disp.getState().setHeading(getHeadingLevel());

	}
	else if (!goingDown && !copied)
	{
		disp.getState().toggleState(HTMLConstants.HEADING,false);
	    //disp.getState().setHeading(getHeadingLevel());fixing is needed,without enclosed tag is working great


	    if (lineChange2())
	    {
		disp.crlf();
	    }
	    else
	    {
		disp.crlf();
		disp.crlf();
	    }
	   // disp.getState().toggleState(HTMLConstants.HEADING);
	//	maybe the above things are needed

	}

	goingDown = !goingDown;
    }

    /** 
     * A method to return whether this Node has a closing tag
     * @Override
     * @return true if this node has a closing tag and false otherwise
     */
    public boolean hasClosing()
    {
	return false;
    }

    /** 
     * A method to return a new copy of this node.
     * @Override
     * @return a new instance of this node with the original name
     */
    public TreeNode copy()
    {
	return new HeadingNode(heading);
    }

    /** 
     * A method to accept a Visitor so that its method can be called to visit this node. 
     * @param theVisitor references the Visitor interface
     * @Override
     */
    public void accept(Visitor theVisitor)
    {
	theVisitor.visit(this);
    }

    /**
     * A method to return the level of heading by referencing a character in the heading string
     * return the level of heading
     */
    public int getHeadingLevel()
    {
	int asciiForZero = 48;
	return heading.charAt(1) - asciiForZero;
    }
}
